package com.xiaoq.distribution.endpoint;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.xiaoq.RedisTemplate4JedisCluster;
import com.xiaoq.coms.GenericDataResponse;
import com.xiaoq.coms.GenericListResponse;
import com.xiaoq.coms.GenericResponse;
import com.xiaoq.coms.util.ConstantUtil;
import com.xiaoq.coms.util.IdGenerator;
import com.xiaoq.distribution.entity.AgreementEntity;
import com.xiaoq.distribution.entity.DistributorEntity;
import com.xiaoq.distribution.service.IAgreementService;
import com.xiaoq.distribution.service.IDistributorService;
import com.xiaoq.distribution.vo.AgreementVo;
import com.xiaoq.vo.PagingVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

@RestController
@RequestMapping(value = "/api/v1/agreement")
@Api(tags={"合同管理相关Api"})
public class AgreementEndpoint {
	
	@Autowired
	private IDistributorService distributorSer;
	
	@Autowired
	private IdGenerator idGenerator;
	
	@Autowired
	private IAgreementService agreementSer;
	
	@Autowired
	private RedisTemplate4JedisCluster redisTemplate;
	
	
	private class AgreementGenericDataResponse extends GenericDataResponse<AgreementVo> {}
	
	private class AgreementGenericListResponse extends GenericListResponse<AgreementVo> {}
	
	@ApiOperation(value = "新增分销合同", notes = "新增分销合同")
	@ApiResponses({ @ApiResponse(code = 200, response = GenericResponse.class, message = "新增分销合同") })
	@PostMapping(value = "", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> addAgreement(@RequestBody AgreementVo agreementVo,HttpServletRequest request) {
		String distribId = agreementVo.getDistribId();
		DistributorEntity distributor = distributorSer.findById(distribId);
		if(distributor==null){
			return GenericResponse.ng("不存在对应的分销商");
		}
		AgreementEntity agreement = new AgreementEntity();
		BeanUtils.copyProperties(agreementVo, agreement);
		agreement.setId(idGenerator.nextId());
		agreement.setDistribName(distributor.getName());
		agreement.setMerchantId(distributor.getMerchantId());
		agreement.setShopId(distributor.getShopId());
		distributor.setIsAgree(ConstantUtil.DISTRIBUTION_NOT_AGREEMENT);
		distributorSer.addDistributor(distributor);
		boolean result = agreementSer.save(agreement);
		if(result){
			return GenericResponse.ok();
		}else{
			return GenericResponse.ng("添加分销商合同失败！");
		}
		
	}
	
	@ApiOperation(value = "编辑回显", notes = "编辑回显")
	@ApiResponses({ @ApiResponse(code = 200, response = AgreementGenericDataResponse.class, message = "编辑回显") })
	@GetMapping(value = "/detail", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> findAgreement(@RequestParam(value="id")@ApiParam(value="合同Id")String id) {
		AgreementEntity agreement = agreementSer.findById(id);
		if(agreement==null){
			return GenericResponse.ng("账号不存在！");
		}
		AgreementVo agreementVo = new AgreementVo();
		BeanUtils.copyProperties(agreement, agreementVo);
		return AgreementGenericDataResponse.okWithData(agreementVo);
	}
	
	@ApiOperation(value = "合同列表", notes = "合同列表")
	@ApiResponses({ @ApiResponse(code = 200, response = AgreementGenericListResponse.class, message = "合同列表") })
	@GetMapping(value = "", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> findAgreements(@ModelAttribute(value = "pvo") PagingVo pvo, HttpServletRequest request) {
		String userId = (String) request.getAttribute("userId");
		String shopId = redisTemplate.get(ConstantUtil.REDIS_PREFIX_MANAGER_SHOP_RELATION, userId);
		List<AgreementEntity> agreeList = agreementSer.findByShopId(shopId, pvo);
		List<AgreementVo> agreeVoList = new ArrayList<>();
		AgreementVo agreementVo = null;
		for(AgreementEntity agreement:agreeList){
			agreementVo = new AgreementVo();
			BeanUtils.copyProperties(agreement, agreementVo);
			agreeVoList.add(agreementVo);
		}
		return AgreementGenericListResponse.listAndCount(agreeVoList, (Long)pvo.getTc());
		
	}
	
	@ApiOperation(value = "编辑合同", notes = "编辑合同")
	@ApiResponses({ @ApiResponse(code = 200, response = GenericResponse.class, message = "编辑合同") })
	@PutMapping(value = "", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> editAgreement(@RequestBody AgreementVo agreementVo,HttpServletRequest request) {
		String id = agreementVo.getId();
		AgreementEntity agreement = agreementSer.findById(id);
		if(agreement==null){
			return GenericResponse.ng("账号不存在！");
		}
		String agreeNum = agreementVo.getAgreeNum();
		if(StringUtils.isNotBlank(agreeNum)){
			agreement.setAgreeNum(agreeNum);
		}
		String distribId = agreementVo.getDistribId();
		if(StringUtils.isNotBlank(distribId)){
			DistributorEntity distributor = distributorSer.findById(distribId);
			agreement.setDistribId(distributor.getId());
			agreement.setDistribName(distributor.getName());
		}
		Long agreeDate = agreementVo.getAgreeDate();
		if(agreeDate!=null){
			agreement.setAgreeDate(agreeDate);
		}
		
		Long startValidity = agreementVo.getStartValidity();
		if(startValidity!=null){
			agreement.setStartValidity(startValidity);
		}
		
		Long endValidity = agreementVo.getEndValidity();
		if(endValidity!=null){
			agreement.setEndValidity(endValidity);
		}
		boolean result = agreementSer.save(agreement);
		if(result){
			return GenericResponse.ok();
		}else{
			return GenericResponse.ng("编辑合同失败！");
		}
	}
	

}
